﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Models;

namespace BookStore.Controllers
{
    public class ShoppingCartController : Controller
    {
        public const string CartSessionKey = "CartID";

        public MVC3BookStoreEntities BookStoreEntities { get; set; }
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            if (BookStoreEntities == null) BookStoreEntities = new MVC3BookStoreEntities();
            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            string SessionKey = Convert.ToString(this.HttpContext.Session[CartSessionKey]);
            var CartItems = from BK in BookStoreEntities.Books
                            join ct in BookStoreEntities.Carts on BK.ID equals ct.BookID
                            where ct.CartID == SessionKey
                            select new CartItems {BookID = BK.ID, CartID = ct.ID,  BookName = BK.BookName, Qty = ct.Qty, Price = BK.Price.Value, Amount = ct.Qty * BK.Price.Value };

            ViewBag.CartItems = CartItems;
            return View();
        }

        public ActionResult Add(int ItemID, string Qty)
        {
            Cart c = new Cart();
            c.BookID = ItemID;
            c.Qty = Convert.ToInt32(Qty);
            c.CartID = this.GetCartId(this.HttpContext);
            c.Amount = c.Qty * this.BookStoreEntities.Books.Where(bk => bk.ID == ItemID).First().Price;
            c.DateCreated = DateTime.Now;
            BookStoreEntities.Carts.AddObject(c);
            
            //Save to Database
            BookStoreEntities.SaveChanges();

            //Redirect to HOME Page
            return RedirectToAction("Index", "Home");
        }

        // We're using HttpContextBase to allow access to cookies.
        private string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                // Generate a new random GUID using System.Guid class
                Guid tempCartId = Guid.NewGuid();

                // Send tempCartId back to client as a cookie
                context.Session[CartSessionKey] = tempCartId.ToString();   
            }

            return context.Session[CartSessionKey].ToString();
        }

        //Delete CART Item
        public ActionResult Delete(int ItemID)
        {
            Cart c = (from cart in this.BookStoreEntities.Carts
                     where cart.ID == ItemID                     
                     select cart).FirstOrDefault();

            //When no items found
            if (c != null)
            {
                this.BookStoreEntities.Carts.DeleteObject(c);
                BookStoreEntities.SaveChanges();
            }

            //Redirect to HOME Page
            return RedirectToAction("Index");
        }

    }
}
